package com.huobao.yunying.adapters;

import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.entity.QuanziComment;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.UserUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class TiedetailAdapter extends QuickAdapter<QuanziComment>{

	private boolean isAll = true;//查看全部
	private String tiezi_id;
	public TiedetailAdapter(Context context, String tiezi_id) {
		super(context, R.layout.item_commit);
		this.tiezi_id = tiezi_id;
	}
	
	public void seeType(boolean isAll){
		this.isAll = isAll;
		notifyDataSetChanged();
	}

	@Override
	protected void convert(BaseAdapterHelper arg0, final QuanziComment arg1) {
		if(TextUtils.isEmpty(arg1.getCid())){
			arg0.setVisible(R.id.tv_11, true);
			arg0.setVisible(R.id.llyt_30, false);
			return;
		}
		arg0.setVisible(R.id.llyt_30, true);
		arg0.setVisible(R.id.tv_11, false);
		RoundImageView img_userphoto = arg0.getView(R.id.img_userphoto);
		//楼主标记
		TextView tv_louzhu = arg0.getView(R.id.tv_louzhu);
		final ImageView image = arg0.getView(R.id.image);
		final ImageView image_sub = arg0.getView(R.id.imageview);
		img_userphoto.setRectAdius(90);
		TextView tv_username = arg0.getView(R.id.tv_username);
		TextView tv_zannum = arg0.getView(R.id.tv_zannum);
		TextView tv_content = arg0.getView(R.id.tv_content);
		View llyt_reply = arg0.getView(R.id.llyt_reply);
		TextView tv_username_sub = arg0.getView(R.id.tv_username_sub);
		TextView tv_content_sub = arg0.getView(R.id.tv_content_sub);
		TextView tv_louceng = arg0.getView(R.id.tv_louceng);
		TextView tv_time = arg0.getView(R.id.tv_time);
		TextView tv_reply = arg0.getView(R.id.tv_reply);
		TextView tv_del = arg0.getView(R.id.tv_del);
		TextView tv_zhulouceng = arg0.getView(R.id.tv_zhulouceng);
		
		if(arg1.getUser_id().equals(String.valueOf(UserUtils.getUserid(context)))){
			tv_reply.setVisibility(View.INVISIBLE);
		}else{
			tv_reply.setVisibility(View.VISIBLE);
		}
		
		if(tiezi_id.equals(arg1.getUser_id())){
			tv_louzhu.setVisibility(View.VISIBLE);
		}else{
			tv_louzhu.setVisibility(View.GONE);
		}
		
		if(arg1.getUser_id().equals(UserUtils.getUserid(context)+"") || tiezi_id.equals(arg1.getUser_id())){
			tv_del.setVisibility(View.VISIBLE);
		}else{
			tv_del.setVisibility(View.INVISIBLE);
		}
		final List<String> imgs = arg1.getImages();
		if(imgs != null && imgs.size() != 0){
			image.setVisibility(View.VISIBLE);
			ImageUtils.loadimg(image, imgs.get(0),new OnImageLoadListener() {
				
				@Override
				public void onLoadSuccess(Drawable drawable) {
					int []  wh = Utils.splitUrl(imgs.get(0));
					int oldWidth = wh[0];
					int oldHight = wh[1];
					int newWidth = ScreenUtil.getInstant().getDisWidth(context) - 15;
					if(oldWidth < newWidth){
						android.widget.RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams();
						layoutParams.width = oldWidth;
						layoutParams.height = oldHight;
						image.setLayoutParams(layoutParams);
						return;
					}
					int newHight = newWidth * oldHight / oldWidth;
					LayoutParams layoutParams = (LayoutParams) image.getLayoutParams();
					layoutParams.width = newWidth;
					layoutParams.height = newHight;
					image.setLayoutParams(layoutParams);
				}
			});
		}
		
		ImageUtils.loadimg(img_userphoto, arg1.getPortrait());
		tv_username.setText(arg1.getUser_name());
		tv_zannum.setText(arg1.getScore());
		tv_content.setText(arg1.getComment());
		if(arg1.getReply() != null){
			final List<String> imgs_sub = arg1.getReply().getImages();
			if(imgs_sub != null && imgs_sub.size() != 0){
				image_sub.setVisibility(View.VISIBLE);
				ImageUtils.loadimg(image_sub, imgs_sub.get(0),new OnImageLoadListener() {
					
					@Override
					public void onLoadSuccess(Drawable drawable) {
						int []  wh = Utils.splitUrl(imgs_sub.get(0));
						int oldWidth = wh[0];
						int oldHight = wh[1];
						int newWidth = ScreenUtil.getInstant().getDisWidth(context) - 15;
						if(oldWidth < newWidth){
							android.widget.RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) image_sub.getLayoutParams();
							layoutParams.width = oldWidth;
							layoutParams.height = oldHight;
							image_sub.setLayoutParams(layoutParams);
							return;
						}
						int newHight = newWidth * oldHight / oldWidth;
						LayoutParams layoutParams = (LayoutParams) image_sub.getLayoutParams();
						layoutParams.width = newWidth;
						layoutParams.height = newHight;
						image_sub.setLayoutParams(layoutParams);
					}
				});
			}
			llyt_reply.setVisibility(View.VISIBLE);
			tv_username_sub.setText(arg1.getReply().getUser_name());
			tv_content_sub.setText(arg1.getReply().getComment());
//			tv_louceng.setText(arg1.getReply().getDateline());
			
			
		}else{
			llyt_reply.setVisibility(View.GONE);
		}
		
		tv_time.setText(arg1.getDateline());
		tv_reply.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(arg1, "togKeyBoard");
			}
		});
		tv_del.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				BaseDialog.TBox("是否删除评论？", context, new BaseDialog.OnDialogSelectListener() {
					@Override
					public void onSelect(String input) {
						if (input.equals("true"))
							delComment(arg1);
					}
				});
			}
		});
		tv_zhulouceng.setText(arg1.getFloor()+"楼");
		if(arg1.getIsfavor() == 1){
			tv_zannum.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_zan_press, 0, 0, 0);
		}else {
			tv_zannum.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_zan, 0, 0, 0);
		}
		tv_zannum.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(arg1.getIsfavor() == 1){
					zan(arg1,true);
				}else{
					zan(arg1,false);
				}
			}
		});
	}

	protected void delComment(final QuanziComment arg1) {
		Map<String, String> map = HttpUtil.getParams("app","postcomment","act","drop","id",arg1.getCid());
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				remove(arg1);
				notifyDataSetChanged();
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	protected void zan(final QuanziComment comment,final boolean isCancel) {
		Map<String, String> map = HttpUtil.getParams("app","postcomment","act","favor","isadd",isCancel?"0":"1","id",comment.getCid());
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				String num = jsonObject.getString("retval");
				comment.setScore(num);
				comment.setIsfavor(isCancel?0:1);
				notifyDataSetChanged();
			}
			
			@Override
			public void onHttpError(String error) {
			}
		});
	}


}
